<script lang="ts" setup>
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import { $http, $system, useStorage } from './utils';
import { useUserStore, useWebConfigStore, useStatusStore } from "@/stores";
import { getConfig } from "./utils/api";
onLaunch((options: any) => {
	uni.hideTabBar();
	const storeage = useStorage();
	const appid = options.query?.appid || $system.appid();
	if (appid) {
		// #ifdef H5
		storeage.set('APPID', appid);
		// #endif
	} else {
		uni.showModal({
			content: '授权地址不正确，请重新访问',
			showCancel: false
			, success() {
				// #ifdef H5
				globalThis.close();
				// #endif
				// #ifdef MP-WEIXIN || MP-TOUTIAO
				uni.navigateBack();
				// #endif
			}
		})
	}
	const statusStore = useStatusStore();
	statusStore.initStatus();
	const { initWebConfig } = useWebConfigStore();
	initWebConfig();
	getConfig();
	$http.get('Ads/list', {
		data: {
			position: 'tabbar'
		}
	}).then((res: any) => {
		if (res.code === $http.ResponseCode.SUCCESS) {
			statusStore.set('tabbar', res.data);
		} else {
			statusStore.resetTabbar();
		}
	}).catch(() => {
		statusStore.resetTabbar();
	})
	const icode = options.query?.icode;
	if (icode && !storeage.get('ICODE.' + appid)) {
		storeage.set('ICODE.' + appid, icode);
	}
	const puid = options.query?.puid;
	if (puid && !storeage.get('PUID.' + appid)) {
		storeage.set('PUID.' + appid, puid);
	}
	// #ifdef MP-WEIXIN || MP-TOUTIAO
	const scene = options.query?.scene;
	if (scene) {
		const sceneArr = decodeURIComponent(scene).split('&');
		const sceneObj: any = {};
		sceneArr.forEach((item: string) => {
			const arr = item.split('=');
			sceneObj[arr[0]] = arr[1];
		})
		if (sceneObj.puid) {
			storeage.set('PUID.' + appid, sceneObj.puid);
		}
	}
	// #endif
	const { setUserInfo, clearUserInfo, initUserInfo, hasLogin } = useUserStore();
	initUserInfo();
	const token = options.query?.token;
	if (token) {
		setUserInfo({ token }).catch(() => { });
	}
	if (hasLogin()) {
		$http.get('User/info').then((res: any) => {
			if (res.code === $http.ResponseCode.SUCCESS) {
				setUserInfo(res.data).catch(() => { });
			} else if (res.code === $http.ResponseCode.NEED_LOGIN) {
				clearUserInfo();
			}
		}).catch(() => {
		})
	}
	console.log('App Launch')
})
onShow(() => {
	console.log('App Show')
})
onHide(() => {
	console.log('App Hide')
})
</script>

<style lang="scss">
// #ifdef MP-WEIXIN || MP-TOUTIAO
view {
	outline: none;
	box-sizing: border-box;
}

// #endif
/*每个页面公共css */
@import "static/common.scss";
</style>
